<template>
  <div class="searc-page">
    <div class="container">
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <transition name="fade-right" mode="out-in">
          <XtxBreadItem>关键字 “{{ $route.query.keyword }}” 的搜索结果：</XtxBreadItem>
        </transition>
      </XtxBread>
      <!-- 商品面板（排序+列表） -->
      <div class="goods-panel">
        <!-- 搜索排序 -->
        <SearchSort @sort-change="changeSort" />
        <!-- 搜索结果列表 -->
        <ul>
          <li v-for="goodsItem in goodsList" :key="goodsItem.id">
            <GoodsItem :goods="goodsItem"/>
          </li>
        </ul>
        <div v-if="!total" class="none">
          <img src="../../assets/images/none.png" alt="" />
          暂无数据
        </div>
        <!-- 分页区 -->
        <XtxPagination
          v-if="total"
          :total="total"
          :page-size="reqParams.pageSize"
          :current-page="reqParams.page"
          @current-change="changePager"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { onMounted, ref } from 'vue'
import { onBeforeRouteUpdate, useRoute } from 'vue-router'
import SearchSort from './components/search-sort.vue'
import GoodsItem from '@/views/category/components/goods-item.vue'
import { findSearchGoods } from '@/api/product'
export default {
  name: 'CategorySearch',
  components: {
    // SearchFilter,
    SearchSort,
    GoodsItem
  },
  setup () {
    const route = useRoute()
    // 商品列表数据
    const goodsList = ref([])
    const total = ref(0)
    // 请求参数
    let reqParams = {
      keyword: route.query.keyword, // 从路由地址获取搜索关键词参数keyword
      page: 1,
      pages: 20
    }
    // 获取数据函数
    const getData = async () => {
      const { result } = await findSearchGoods(reqParams)
      goodsList.value = result.pageData.items
      total.value = result.pageData.counts
    }
    onMounted(() => {
      // 组件挂载后获取数据
      getData()
    })
    // 分页切换
    const changePager = (newPage) => {
      reqParams.page = newPage
      getData()
    }
    // 无数据的暂时，根据total判断即可
    // 排序条件改变更新数据
    // 子组件通过自定义事件传递数据给父组件，拿到排序数据给查询参数重新请求（第一页）即可
    const changeSort = (sortParams) => {
      // 合并请求参数，保留之前参数
      // { ...reqParams, ...sortParams } reqParams保留之前的参数，sortParams合并筛选后的参数
      reqParams = { ...reqParams, ...sortParams }
      reqParams.page = 1
      getData()
    }
    // 搜索关键字改变更新数据
    // 监听路由参数的变化，拿着新的关键字且排序需要重置，重新发请求（第一页）即可
    // 排序组件的排序效果需要重置
    onBeforeRouteUpdate((to, from, next) => {
      reqParams.keyword = to.query.keyword
      reqParams.sortField = null
      reqParams.sortMethod = null
      reqParams.page = 1
      getData()
      next()
    })
    return { reqParams, goodsList, changePager, total, changeSort }
  }
}
</script>

<style lang="less" scoped>
.goods-panel {
  background: #fff;
  padding: 0 25px;
  ul {
    display: flex;
    flex-wrap: wrap;
    li {
      margin-right: 20px;
      margin-bottom: 20px;
      &:nth-child(5n) {
        margin-right: 0;
      }
      .goods-item {
        display: block;
        width: 220px;
        padding: 20px 30px;
        text-align: center;
      .hoverShadow();
        img {
          width: 160px;
          height: 160px;
        }
        p {
          padding-top: 10px;
        }
        .name {
          font-size: 16px;
        }
        .desc {
          color: #999;
          height: 29px;
        }
        .price {
          color: @priceColor;
          font-size: 20px;
        }
      }
    }
  }
}
.none {
  padding: 100px 0;
  text-align: center;
  color: #999;
  img {
    width: 100px;
    height: 100px;
    margin-right: 10px;
    object-fit: contain;
  }
}
</style>
